﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using 基于WPF的电缆采样数据分析系统.Service;
using static SkiaSharp.HarfBuzz.SKShaper;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.CommonComponent
{
    public class CalculatorUCViewModel : BindableBase, IDialogHostAware
    {
        // 单例模式
        public static CalculatorUCViewModel Instance = new CalculatorUCViewModel();

        // 主机名
        private const string DialogHostName = "RootDialog";

        #region 计算器信息

        /// <summary>
        /// 计算器的值
        /// </summary>
        private string _CurrentCalculatorValue = "0";

        /// <summary>
        /// 计算器的值
        /// </summary>
        public string CurrentCalculatorValue
        {
            get { return _CurrentCalculatorValue; }
            set { 
                if (value == "-0")
                {
                    _CurrentCalculatorValue = "0";
                    RaisePropertyChanged();
                    return;
                }
                _CurrentCalculatorValue = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 单位信息
        /// </summary>
        private string _UnitInfo;

        /// <summary>
        /// 单位信息
        /// </summary>
        public string UnitInfo
        {
            get { return _UnitInfo; }
            set { 
                _UnitInfo = value;
                RaisePropertyChanged();
            }
        }


        #endregion

        public CalculatorUCViewModel()
        {
            // 绑定确定和取消命令
            SaveCommand = new DelegateCommand(SaveFun);
            CancelCommand = new DelegateCommand(CancelFun);

            // 绑定按钮命令
            CalculatorBtnCmm = new DelegateCommand<CalculatorBtnType?>(CalculatorBtnFun);
        }

        #region 确定
        /// <summary>
        /// 确定命令
        /// </summary>
        public DelegateCommand SaveCommand { get; set; }

        private void SaveFun()
        {
            var paras = new DialogParameters();
            paras.Add("CurrentCalculatorValue", CurrentCalculatorValue);
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, paras));
            }
        }

        #endregion

        #region 取消
        public DelegateCommand CancelCommand { get; set; }

        private void CancelFun()
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
            }
        }

        #endregion

        /// <summary>
        /// 打开过程中
        /// </summary>
        /// <param name="parameters"></param>
        public void OnDialogOpening(IDialogParameters parameters)
        {
            UnitInfo = parameters.GetValue<string>("UnitValue");
            CurrentCalculatorValue = parameters.GetValue<float>("ComputeValue").ToString();
        }

        #region 按钮操作
        /// <summary>
        /// 按钮操作命令
        /// </summary>
        public DelegateCommand<CalculatorBtnType?> CalculatorBtnCmm { get; set; }

        /// <summary>
        /// 按钮操作方法
        /// </summary>
        private void CalculatorBtnFun(CalculatorBtnType? calculatorBtnType)
        {
            // 当按键值为C时，归零
            if (calculatorBtnType == CalculatorBtnType.MakeZero)
            {
                CurrentCalculatorValue = "0";
                return;
            }

            if (calculatorBtnType == CalculatorBtnType.PointBtn)
            {
                if (CurrentCalculatorValue.Contains("."))
                {
                    return;
                }

                CurrentCalculatorValue += ".";
                return;
            }

            // 一位数
            if (CurrentCalculatorValue.Length == 1)
            {
                // 当前值为0时按键操作
                if (string.Equals(CurrentCalculatorValue, "0"))
                {
                    CurrentValueEquelsZero(calculatorBtnType);
                    return;
                }

                // 当前值为1 - 9时按键操作
                CurrentValueEquelsOneToNine(calculatorBtnType);
                return;
            }
            
            // 两位数
            if (CurrentCalculatorValue.Length == 2)
            {
                var conversionResult = int.TryParse(CurrentCalculatorValue, out int number);

                // 是否为数字
                if (conversionResult)
                {
                    // 当前值为10 - 99时按键操作
                    if (number >= 10 && number <= 99)
                    {
                        CurrentValueEquelsTenToNinetyNine(calculatorBtnType);
                        return;
                    }

                    // 当前值为-1 - -9时按键操作
                    if (number >= -9 && number <= -1)
                    {
                        CurrentValueEquelsMinusOneToMinusNine(calculatorBtnType);
                        return;
                    }

                    
                }


                // 当前值为0. - 9.时按键操作
                if (string.Equals(CurrentCalculatorValue.Substring(CurrentCalculatorValue.Length - 1), "."))
                {
                    CurrentValueEquelsOnePointToNinePoint(calculatorBtnType);
                    return;
                }

                // 当前值为
            }

            // 三位数及以上
            if (CurrentCalculatorValue.Length >= 3)
            {
                var conversionResult = float.TryParse(CurrentCalculatorValue, out float number);

                // 是否为数字
                if (conversionResult)
                {
                    CurrentValueEquelsThreeAndIsNumber(calculatorBtnType, number);
                    return;
                }

                CurrentValueEquelsThreeAndIsNotNumber(calculatorBtnType);
                return;
            }
        }

        /// <summary>
        /// 当前值为0时按键操作
        /// </summary>
        /// <param name="calculatorBtnType">按键值</param>
        private void CurrentValueEquelsZero(CalculatorBtnType? calculatorBtnType)
        {
            switch (calculatorBtnType)
            {
                case CalculatorBtnType.LeftBtn:
                    CurrentCalculatorValue = "0";
                    break;
                case CalculatorBtnType.RightBtn:
                    CurrentCalculatorValue = "0";
                    break;
                case CalculatorBtnType.SevenBtn:
                    CurrentCalculatorValue = "7";
                    break;
                case CalculatorBtnType.EightBtn:
                    CurrentCalculatorValue = "8";
                    break;
                case CalculatorBtnType.NineBtn:
                    CurrentCalculatorValue = "9";
                    break;
                case CalculatorBtnType.FourBtn:
                    CurrentCalculatorValue = "4";
                    break;
                case CalculatorBtnType.FiveBtn:
                    CurrentCalculatorValue = "5";
                    break;
                case CalculatorBtnType.SixBtn:
                    CurrentCalculatorValue = "6";
                    break;
                case CalculatorBtnType.OneBtn:
                    CurrentCalculatorValue = "1";
                    break;
                case CalculatorBtnType.TwoBtn:
                    CurrentCalculatorValue = "2";
                    break;
                case CalculatorBtnType.ThreeBtn:
                    CurrentCalculatorValue = "3";
                    break;
                case CalculatorBtnType.ZeroBtn:
                    CurrentCalculatorValue = "0";
                    break;
                case CalculatorBtnType.PlusOrMinusBtn:
                    CurrentCalculatorValue = "0";
                    break;
            }
        }



        /// <summary>
        /// 当前值为1 - 9时按键操作
        /// </summary>
        /// <param name="calculatorBtnType">按键值</param>
        private void CurrentValueEquelsOneToNine(CalculatorBtnType? calculatorBtnType)
        {
            switch (calculatorBtnType)
            {
                case CalculatorBtnType.LeftBtn:
                    CurrentCalculatorValue = "0";
                    break;
                case CalculatorBtnType.RightBtn:
                    CurrentCalculatorValue = "0";
                    break;
                case CalculatorBtnType.SevenBtn:
                    CurrentCalculatorValue += "7";
                    break;
                case CalculatorBtnType.EightBtn:
                    CurrentCalculatorValue += "8";
                    break;
                case CalculatorBtnType.NineBtn:
                    CurrentCalculatorValue += "9";
                    break;
                case CalculatorBtnType.FourBtn:
                    CurrentCalculatorValue += "4";
                    break;
                case CalculatorBtnType.FiveBtn:
                    CurrentCalculatorValue += "5";
                    break;
                case CalculatorBtnType.SixBtn:
                    CurrentCalculatorValue += "6";
                    break;
                case CalculatorBtnType.OneBtn:
                    CurrentCalculatorValue += "1";
                    break;
                case CalculatorBtnType.TwoBtn:
                    CurrentCalculatorValue += "2";
                    break;
                case CalculatorBtnType.ThreeBtn:
                    CurrentCalculatorValue += "3";
                    break;
                case CalculatorBtnType.ZeroBtn:
                    CurrentCalculatorValue += "0";
                    break;
                case CalculatorBtnType.PlusOrMinusBtn:
                    CurrentCalculatorValue = string.Concat("-", CurrentCalculatorValue);
                    break;
            }
        }

        /// <summary>
        /// 当前值为10 - 99时按键操作
        /// </summary>
        /// <param name="calculatorBtnType">按键值</param>
        private void CurrentValueEquelsTenToNinetyNine(CalculatorBtnType? calculatorBtnType)
        {
            switch (calculatorBtnType)
            {
                case CalculatorBtnType.LeftBtn:
                    CurrentCalculatorValue = CurrentCalculatorValue.Substring(1);
                    break;
                case CalculatorBtnType.RightBtn:
                    CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 1);
                    break;
                case CalculatorBtnType.SevenBtn:
                    CurrentCalculatorValue += "7";
                    break;
                case CalculatorBtnType.EightBtn:
                    CurrentCalculatorValue += "8";
                    break;
                case CalculatorBtnType.NineBtn:
                    CurrentCalculatorValue += "9";
                    break;
                case CalculatorBtnType.FourBtn:
                    CurrentCalculatorValue += "4";
                    break;
                case CalculatorBtnType.FiveBtn:
                    CurrentCalculatorValue += "5";
                    break;
                case CalculatorBtnType.SixBtn:
                    CurrentCalculatorValue += "6";
                    break;
                case CalculatorBtnType.OneBtn:
                    CurrentCalculatorValue += "1";
                    break;
                case CalculatorBtnType.TwoBtn:
                    CurrentCalculatorValue += "2";
                    break;
                case CalculatorBtnType.ThreeBtn:
                    CurrentCalculatorValue += "3";
                    break;
                case CalculatorBtnType.ZeroBtn:
                    CurrentCalculatorValue += "0";
                    break;
                case CalculatorBtnType.PlusOrMinusBtn:
                    CurrentCalculatorValue = string.Concat("-", CurrentCalculatorValue);
                    break;
            }
        }

        /// <summary>
        /// 当前值为-1 - -9时按键操作
        /// </summary>
        /// <param name="calculatorBtnType">按键值</param>
        private void CurrentValueEquelsMinusOneToMinusNine(CalculatorBtnType? calculatorBtnType)
        {
            switch (calculatorBtnType)
            {
                case CalculatorBtnType.LeftBtn:
                    CurrentCalculatorValue = CurrentCalculatorValue.Substring(1);
                    break;
                case CalculatorBtnType.RightBtn:
                    CurrentCalculatorValue = "0";
                    break;
                case CalculatorBtnType.SevenBtn:
                    CurrentCalculatorValue += "7";
                    break;
                case CalculatorBtnType.EightBtn:
                    CurrentCalculatorValue += "8";
                    break;
                case CalculatorBtnType.NineBtn:
                    CurrentCalculatorValue += "9";
                    break;
                case CalculatorBtnType.FourBtn:
                    CurrentCalculatorValue += "4";
                    break;
                case CalculatorBtnType.FiveBtn:
                    CurrentCalculatorValue += "5";
                    break;
                case CalculatorBtnType.SixBtn:
                    CurrentCalculatorValue += "6";
                    break;
                case CalculatorBtnType.OneBtn:
                    CurrentCalculatorValue += "1";
                    break;
                case CalculatorBtnType.TwoBtn:
                    CurrentCalculatorValue += "2";
                    break;
                case CalculatorBtnType.ThreeBtn:
                    CurrentCalculatorValue += "3";
                    break;
                case CalculatorBtnType.ZeroBtn:
                    CurrentCalculatorValue += "0";
                    break;
                case CalculatorBtnType.PlusOrMinusBtn:
                    CurrentCalculatorValue = CurrentCalculatorValue.Replace("-", "");
                    break;
            }
        }

        /// <summary>
        /// 当前值为-0
        /// </summary>
        private void CurrentValueEquelsMinusZero()
        {
            if (CurrentCalculatorValue == "-0")
            {
                CurrentCalculatorValue = "0";
            }
        }


        /// <summary>
        /// 当前值为0. - 9.时按键操作
        /// </summary>
        /// <param name="calculatorBtnType">按键值</param>
        private void CurrentValueEquelsOnePointToNinePoint(CalculatorBtnType? calculatorBtnType)
        {
            switch (calculatorBtnType)
            {
                case CalculatorBtnType.LeftBtn:
                    CurrentCalculatorValue = "0";
                    break;
                case CalculatorBtnType.RightBtn:
                    CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 1);
                    break;
                case CalculatorBtnType.SevenBtn:
                    CurrentCalculatorValue += "7";
                    break;
                case CalculatorBtnType.EightBtn:
                    CurrentCalculatorValue += "8";
                    break;
                case CalculatorBtnType.NineBtn:
                    CurrentCalculatorValue += "9";
                    break;
                case CalculatorBtnType.FourBtn:
                    CurrentCalculatorValue += "4";
                    break;
                case CalculatorBtnType.FiveBtn:
                    CurrentCalculatorValue += "5";
                    break;
                case CalculatorBtnType.SixBtn:
                    CurrentCalculatorValue += "6";
                    break;
                case CalculatorBtnType.OneBtn:
                    CurrentCalculatorValue += "1";
                    break;
                case CalculatorBtnType.TwoBtn:
                    CurrentCalculatorValue += "2";
                    break;
                case CalculatorBtnType.ThreeBtn:
                    CurrentCalculatorValue += "3";
                    break;
                case CalculatorBtnType.ZeroBtn:
                    CurrentCalculatorValue += "0";
                    break;
                case CalculatorBtnType.PlusOrMinusBtn:
                    //值为0.
                    if (string.Equals(CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 1), "0"))
                    {
                        CurrentCalculatorValue = "0";
                        break;
                    }

                    // 值为1. - 9.
                    CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 1);
                    CurrentCalculatorValue = string.Concat("-", CurrentCalculatorValue);
                    break;
            }
        }

        /// <summary>
        /// 当前值为三个字符且为数字
        /// </summary>
        /// <param name="calculatorBtnType"></param>
        private void CurrentValueEquelsThreeAndIsNumber(CalculatorBtnType? calculatorBtnType, float number)
        {
            // 值小于0时
            if (number < 0)
            {
                switch (calculatorBtnType)
                {
                    case CalculatorBtnType.LeftBtn:
                        CurrentCalculatorValue = CurrentCalculatorValue.Substring(1);
                        break;
                    case CalculatorBtnType.RightBtn:
                        if (CurrentCalculatorValue.IndexOf(".") == (CurrentCalculatorValue.Length - 2))
                        {
                            CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 2);
                            break;
                        }

                        CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 1);
                        break;
                    case CalculatorBtnType.SevenBtn:
                        CurrentCalculatorValue += "7";
                        break;
                    case CalculatorBtnType.EightBtn:
                        CurrentCalculatorValue += "8";
                        break;
                    case CalculatorBtnType.NineBtn:
                        CurrentCalculatorValue += "9";
                        break;
                    case CalculatorBtnType.FourBtn:
                        CurrentCalculatorValue += "4";
                        break;
                    case CalculatorBtnType.FiveBtn:
                        CurrentCalculatorValue += "5";
                        break;
                    case CalculatorBtnType.SixBtn:
                        CurrentCalculatorValue += "6";
                        break;
                    case CalculatorBtnType.OneBtn:
                        CurrentCalculatorValue += "1";
                        break;
                    case CalculatorBtnType.TwoBtn:
                        CurrentCalculatorValue += "2";
                        break;
                    case CalculatorBtnType.ThreeBtn:
                        CurrentCalculatorValue += "3";
                        break;
                    case CalculatorBtnType.ZeroBtn:
                        CurrentCalculatorValue += "0";
                        break;
                    case CalculatorBtnType.PlusOrMinusBtn:
                        CurrentCalculatorValue = CurrentCalculatorValue.Replace("-", "");
                        break;
                }
            }

            // 值小于10时
            if (number < 10 && number >= 0)
            {
                switch (calculatorBtnType)
                {
                    case CalculatorBtnType.LeftBtn:
                        CurrentCalculatorValue = "0";
                        break;
                    case CalculatorBtnType.RightBtn:
                        if (CurrentCalculatorValue.IndexOf(".") == (CurrentCalculatorValue.Length - 2))
                        {
                            CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 2);
                            break;
                        }

                        CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 1);
                        break;
                    case CalculatorBtnType.SevenBtn:
                        CurrentCalculatorValue += "7";
                        break;
                    case CalculatorBtnType.EightBtn:
                        CurrentCalculatorValue += "8";
                        break;
                    case CalculatorBtnType.NineBtn:
                        CurrentCalculatorValue += "9";
                        break;
                    case CalculatorBtnType.FourBtn:
                        CurrentCalculatorValue += "4";
                        break;
                    case CalculatorBtnType.FiveBtn:
                        CurrentCalculatorValue += "5";
                        break;
                    case CalculatorBtnType.SixBtn:
                        CurrentCalculatorValue += "6";
                        break;
                    case CalculatorBtnType.OneBtn:
                        CurrentCalculatorValue += "1";
                        break;
                    case CalculatorBtnType.TwoBtn:
                        CurrentCalculatorValue += "2";
                        break;
                    case CalculatorBtnType.ThreeBtn:
                        CurrentCalculatorValue += "3";
                        break;
                    case CalculatorBtnType.ZeroBtn:
                        CurrentCalculatorValue += "0";
                        break;
                    case CalculatorBtnType.PlusOrMinusBtn:
                        CurrentCalculatorValue = string.Concat("-", CurrentCalculatorValue);
                        break;
                }
            }

            // 值大于10
            if (number >= 10)
            {
                switch (calculatorBtnType)
                {
                    case CalculatorBtnType.LeftBtn:
                        CurrentCalculatorValue = CurrentCalculatorValue.Substring(1);
                        break;
                    case CalculatorBtnType.RightBtn:
                        if (CurrentCalculatorValue.IndexOf(".") == (CurrentCalculatorValue.Length - 2))
                        {
                            CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 2);
                            break;
                        }

                        CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 1);
                        break;
                    case CalculatorBtnType.SevenBtn:
                        CurrentCalculatorValue += "7";
                        break;
                    case CalculatorBtnType.EightBtn:
                        CurrentCalculatorValue += "8";
                        break;
                    case CalculatorBtnType.NineBtn:
                        CurrentCalculatorValue += "9";
                        break;
                    case CalculatorBtnType.FourBtn:
                        CurrentCalculatorValue += "4";
                        break;
                    case CalculatorBtnType.FiveBtn:
                        CurrentCalculatorValue += "5";
                        break;
                    case CalculatorBtnType.SixBtn:
                        CurrentCalculatorValue += "6";
                        break;
                    case CalculatorBtnType.OneBtn:
                        CurrentCalculatorValue += "1";
                        break;
                    case CalculatorBtnType.TwoBtn:
                        CurrentCalculatorValue += "2";
                        break;
                    case CalculatorBtnType.ThreeBtn:
                        CurrentCalculatorValue += "3";
                        break;
                    case CalculatorBtnType.ZeroBtn:
                        CurrentCalculatorValue += "0";
                        break;
                    case CalculatorBtnType.PlusOrMinusBtn:
                        CurrentCalculatorValue = string.Concat("-", CurrentCalculatorValue);
                        break;
                }
            }
        }

        /// <summary>
        /// 当前值为三个字符且为非数字
        /// </summary>
        /// <param name="calculatorBtnType"></param>
        private void CurrentValueEquelsThreeAndIsNotNumber(CalculatorBtnType? calculatorBtnType)
        {
            switch (calculatorBtnType)
            {
                case CalculatorBtnType.LeftBtn:
                    CurrentCalculatorValue = CurrentCalculatorValue.Substring(1);
                    break;
                case CalculatorBtnType.RightBtn:
                    if (CurrentCalculatorValue.IndexOf(".") == (CurrentCalculatorValue.Length - 2))
                    {
                        CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 2);
                        break;
                    }

                    CurrentCalculatorValue = CurrentCalculatorValue.Substring(0, CurrentCalculatorValue.Length - 1);
                    break;
                case CalculatorBtnType.SevenBtn:
                    CurrentCalculatorValue += "7";
                    break;
                case CalculatorBtnType.EightBtn:
                    CurrentCalculatorValue += "8";
                    break;
                case CalculatorBtnType.NineBtn:
                    CurrentCalculatorValue += "9";
                    break;
                case CalculatorBtnType.FourBtn:
                    CurrentCalculatorValue += "4";
                    break;
                case CalculatorBtnType.FiveBtn:
                    CurrentCalculatorValue += "5";
                    break;
                case CalculatorBtnType.SixBtn:
                    CurrentCalculatorValue += "6";
                    break;
                case CalculatorBtnType.OneBtn:
                    CurrentCalculatorValue += "1";
                    break;
                case CalculatorBtnType.TwoBtn:
                    CurrentCalculatorValue += "2";
                    break;
                case CalculatorBtnType.ThreeBtn:
                    CurrentCalculatorValue += "3";
                    break;
                case CalculatorBtnType.ZeroBtn:
                    CurrentCalculatorValue += "0";
                    break;
                case CalculatorBtnType.PlusOrMinusBtn:
                    if (string.Equals(CurrentCalculatorValue.Substring(0, 1), "-"))
                    {
                        CurrentCalculatorValue = CurrentCalculatorValue.Replace("-", "");
                        break;
                    } else
                    {
                        CurrentCalculatorValue = string.Concat("-", CurrentCalculatorValue);
                        break;
                    }
            }
        }
        #endregion
    }
}
